package com.abc.thread;

import java.util.concurrent.CountDownLatch;

public class WorkThread implements Runnable {

   private volatile boolean exit = false;

   private CountDownLatch latch;

   private int index;

   private Long start;

   @Override
   public void run() {

      Long end = System.currentTimeMillis();

      if ((end - start) >= 1000) {
         //在队列中已等待1s，不执行了
         System.out.println("线程" + Thread.currentThread() + "在队列中已等待1s，不执行了");
      }
      else {
         if (exit) {
            System.out.println("主线程跑了，次线程不用做");
            return;
         }

         System.out.println("线程" + Thread.currentThread() + ",干活中");

//         try {
//            Thread.sleep(4000);
//         }
//         catch (Exception e) {
//         }

         if (exit) {
            System.out.println("主线程跑了，次线程不用做");
            return;
         }
         else {
            //再干其他活
            System.out.println("线程" + Thread.currentThread() + ",干活index" + index);
         }
      }

      latch.countDown();
   }

   public void setLatch(CountDownLatch latch) {
      this.latch = latch;
   }

   public void setIndex(int index) {
      this.index = index;
   }

   public void setStart(Long start) {
      this.start = start;
   }

   public void setExit(boolean exit) {
      this.exit = exit;
   }
}
